
/**
 *
 */
package com.serviceprovider.business.module.admin.transaction.utility;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.model.NewCsvReplica;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

/**
 * @author mrduongnv@gmail.com
 *
 */
public class SuccessfulDataTag implements Tag {

    /** The parent. */
    private Tag parent = null;

    /** The pc. */
    private PageContext pc          = null;
    List<NewCsvReplica> successList = null;

    /*
     * (non-Javadoc)
     *
     * @see
     * javax.servlet.jsp.tagext.Tag#setPageContext(javax.servlet.jsp.PageContext
     * )
     */
    public void setPageContext(PageContext p) {
        this.pc = p;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#setParent(javax.servlet.jsp.tagext.Tag)
     */
    public void setParent(Tag t) {
        this.parent = t;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#getParent()
     */
    public Tag getParent() {
        return this.parent;
    }

    /**
     * Gets the success list.
     *
     * @return the successList
     */
    public List<NewCsvReplica> getSuccessList() {
        return this.successList;
    }

    /**
     * Sets the success list.
     *
     * @param successList
     *            the successList to set
     */
    public void setSuccessList(List<NewCsvReplica> successList) {
        this.successList = successList;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
     */
    public int doStartTag() throws JspException {
        try {
            if (this.successList != null) {
                this.pc.getOut().write(createErrorTable(this.successList));
            }
        } catch (IOException e) {
            throw new JspTagException("An IOException occurred.");
        }

        return SKIP_BODY;
    }

    private String createErrorTable(List<NewCsvReplica> successDataList) {
        StringBuilder builder = new StringBuilder();

        builder.append("<table width=\"100%\" class=\"formTable\">");
        builder.append("<tbody>");
        builder.append("<tr class=\"formHeader\">");
        builder.append("<td>Trace Number</td>");
        builder.append("<td>Receiver Account</td>");
        builder.append("<td>Receiver Name</td>");
        builder.append("<td>Originator Name</td>");
        builder.append("<td>Transaction Amount</td>");

        // builder.append("<td>SSN</td>");
        builder.append("<td>Originator Code</td>");
        builder.append("</tr>");

        for (NewCsvReplica transactionCSV : successDataList) {
            createTR(builder, transactionCSV);
        }

        builder.append("</tbody>");
        builder.append("</table>");

        return builder.toString();
    }

    private void createTR(StringBuilder builder, NewCsvReplica transactionCSV) {
        builder.append("<tr>");
        createTD(builder, transactionCSV);
        builder.append("</tr>");
    }

    private void createTD(StringBuilder builder, NewCsvReplica transactionCSV) {
        createRecord(builder, (transactionCSV.getTrace_number() == null)
                              ? ""
                              : transactionCSV.getTrace_number());
        createRecord(builder, (transactionCSV.getService_provider_id() == null)
                              ? ""
                              : transactionCSV.getService_provider_id());
        createRecord(builder, (transactionCSV.getReceiver_name() == null)
                              ? ""
                              : transactionCSV.getReceiver_name());
        createRecord(builder, (transactionCSV.getOriginator_name() == null)
                              ? ""
                              : transactionCSV.getOriginator_name());
        createRecord(builder, (transactionCSV.getTransaction_amount() == null)
                              ? ""
                              : transactionCSV.getTransaction_amount());
        createRecord(builder, (transactionCSV.getOrginator_code() == null)
                              ? ""
                              : transactionCSV.getOrginator_code());
    }

    private void createRecord(StringBuilder builder, String string) {
        builder.append("<td>");
        builder.append(string);
        builder.append("</td>");
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.servlet.jsp.tagext.Tag#release()
     */
    public void release() {
        this.pc          = null;
        this.parent      = null;
        this.successList = null;
    }
}



